package com.aaa.cars.controller;

import com.aaa.cars.entity.Brand;
import com.aaa.cars.entity.Car;
import com.aaa.cars.service.BrandService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Brand)表控制层
 *
 * @author makejava
 * @since 2020-11-17 20:37:38
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    /**
     * 服务对象
     */
    @Resource
    private BrandService brandService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.brandService.queryById(id));
    }

    /**
     * 全部新增
     * @param brand
     * @return
     */
    @PostMapping("insertBrand")
    public Result insertBrand(@RequestBody Brand brand){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),brandService.insert(brand));
    }

    /**
     * 修改数据
     * @param brand
     * @return
     */
    @PutMapping("updateBrand")
    public Result updateBrand(@RequestBody Brand brand){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),brandService.update(brand));
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteBrand")
    public Result deleteBrand(Integer id){
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),brandService.deleteById(id));
    }

    /**
     * 分页和模糊查询
     * @param pageNo
     * @param pageSize
     * @param brand
     * @return
     */
    @GetMapping("queryAll")
    public Result queryAll(Integer pageNo, Integer pageSize, Brand brand){
        return brandService.queryAll(pageNo,pageSize,brand);
    }


}